<?php


namespace app\plugins\MemberPointCashSettlement;

class Client extends \app\api\controller\MemberApp
{
	public function __construct()
	{
		$args = func_get_args();
		if ($args) {
			parent::__construct(...$args);
		} else {
			parent::__construct();
		}
		$Common = new Common();
		$Common->check_plug_auth();
	}
	public function get_settlement_list()
	{
		list($verify_code, $user) = self::verify_user($this->project, $this->apiInfo, $this->apiConfig, $this->input, new \app\api\common\entity\MemberAppUserVerify());
		if ($verify_code !== \app\api\controller\Code::success) {
			$this->response($verify_code, $user);
		}
		$page = $this->page_no;
		$limit = $this->page_size;
		$where[] = ["uid", "=", $user["id"]];
		$list = \think\Db::name("app_member_user_settlement")->where($where)->order("id", "desc")->field("id,type,amount,arrival,settlement_info,service_fee,status,reject_reason,apply_time,solve_time")->page($page, $limit)->select();
		$count = \think\Db::name("app_member_user_settlement")->where($where)->count();
		$pages = ceil($count / $limit);
		$data = [];
		foreach ($list as $item) {
			$item["settlement_info"] = json_decode($item["settlement_info"], true);
			$data[] = $item;
		}
		$this->response(\app\api\controller\Code::success, "获取成功", ["list" => $data, "pages" => $pages, "count" => $count, "page" => $page]);
	}
	public function get_settlement_config()
	{
		$member_app_config = json_decode($this->project["member_app_config"], true);
		$this->response(\app\api\controller\Code::success, "获取成功", $member_app_config["settlement_config"] ?: []);
	}
	public function get_settlement_info()
	{
		$validate = \think\Validate::make(["user_id" => "require", "user_token" => "require"], ["user_id.require" => "用户ID 不能为空！", "user_token.require" => "用户token 不能为空！"]);
		if (!$validate->check($this->input)) {
			$this->response(\app\api\controller\Code::fail, $validate->getError());
		}
		list($verify_code, $user) = self::verify_user($this->project, $this->apiInfo, $this->apiConfig, $this->input, new \app\api\common\entity\MemberAppUserVerify());
		if ($verify_code !== \app\api\controller\Code::success) {
			$this->response($verify_code, $user);
		}
		$config = json_decode($user["settlement_info"], true);
		$data = ["wechat" => ["account" => $config["wechat"]["account"] ?? "", "name" => $config["wechat"]["name"] ?? "", "qrcode" => $config["wechat"]["qrcode"] ?? ""], "alipay" => ["account" => $config["alipay"]["account"] ?? "", "name" => $config["alipay"]["name"] ?? "", "qrcode" => $config["alipay"]["qrcode"] ?? ""], "bankcard" => ["bank" => $config["bankcard"]["bank"] ?? "", "account" => $config["bankcard"]["account"] ?? "", "name" => $config["bankcard"]["name"] ?? ""]];
		$this->response(\app\api\controller\Code::success, "获取成功", ["settlement_info" => $data]);
	}
	public function revise_settlement_info()
	{
		$validate = \think\Validate::make(["user_id" => "require", "user_token" => "require"], ["user_id.require" => "用户ID 不能为空！", "user_token.require" => "用户token 不能为空！"]);
		if (!$validate->check($this->input)) {
			$this->response(\app\api\controller\Code::fail, $validate->getError());
		}
		list($verify_code, $user) = self::verify_user($this->project, $this->apiInfo, $this->apiConfig, $this->input, new \app\api\common\entity\MemberAppUserVerify());
		if ($verify_code !== \app\api\controller\Code::success) {
			$this->response($verify_code, $user);
		}
		$data = ["wechat" => ["account" => $this->input["wechat_account"] ?? "", "name" => $this->input["wechat_name"] ?? "", "qrcode" => $this->input["wechat_qrcode"] ?? ""], "alipay" => ["account" => $this->input["alipay_account"] ?? "", "name" => $this->input["alipay_name"] ?? "", "qrcode" => $this->input["alipay_qrcode"] ?? ""], "bankcard" => ["bank" => $this->input["bankcard_bank"] ?? "", "account" => $this->input["bankcard_account"] ?? "", "name" => $this->input["bankcard_name"] ?? ""]];
		\think\Db::name("app_member_user")->where(["id" => $user["id"]])->update(["settlement_info" => json_encode($data, JSON_UNESCAPED_UNICODE)]);
		$this->response(\app\api\controller\Code::success, "修改成功");
	}
	public function apply_point_cash_settlement()
	{
		$validate = \think\Validate::make(["user_id" => "require", "user_token" => "require", "type" => "require"], ["user_id.require" => "用户ID 不能为空！", "user_token.require" => "用户token 不能为空！", "type.require" => "结算方式 不能为空！"]);
		if (!$validate->check($this->input)) {
			$this->response(\app\api\controller\Code::fail, $validate->getError());
		}
		list($verify_code, $user) = self::verify_user($this->project, $this->apiInfo, $this->apiConfig, $this->input, new \app\api\common\entity\MemberAppUserVerify());
		if ($verify_code !== \app\api\controller\Code::success) {
			$this->response($verify_code, $user);
		}
		$member_app_config = json_decode($this->project["member_app_config"], true);
		$config = $member_app_config["settlement_config"] ?: [];
		$type = $this->input["type"];
		if (empty($member_app_config["point_config"]["cash_settlement_open"])) {
			$this->response(\app\api\controller\Code::fail, "未开启积分现金结算功能");
		}
		if (empty($member_app_config["point_config"]["cash_settlement_ratio"])) {
			$this->response(\app\api\controller\Code::fail, "未设置积分结算比例");
		}
		$amount = round($user["point"] * $member_app_config["point_config"]["cash_settlement_ratio"], 2);
		$service_fee = round($config["service_charge"] / 100 * $amount, 2);
		$arrival = $amount - $service_fee;
		$user_settlement_info = json_decode($user["settlement_info"], true);
		if ($type == 0) {
			if ($config["method"]["bankcard"]["open"] == 0) {
				$this->response(\app\api\controller\Code::fail, "未开启银行卡结算渠道");
			}
			$minimum = $config["method"]["bankcard"]["minimum"];
			$maximum = $config["method"]["bankcard"]["maximum"];
			if (empty($user_settlement_info["bankcard"]["account"]) || empty($user_settlement_info["bankcard"]["name"]) || empty($user_settlement_info["bankcard"]["bank"])) {
				$this->response(\app\api\controller\Code::fail, "您未设置银行卡结算信息");
			}
		} elseif ($type == 1) {
			if ($config["method"]["wechat"]["open"] == 0) {
				$this->response(\app\api\controller\Code::fail, "未开启微信零钱结算渠道");
			}
			$minimum = $config["method"]["wechat"]["minimum"];
			$maximum = $config["method"]["wechat"]["maximum"];
			if (empty($user_settlement_info["wechat"]["account"]) || empty($user_settlement_info["wechat"]["name"]) || empty($user_settlement_info["wechat"]["qrcode"])) {
				$this->response(\app\api\controller\Code::fail, "您未设置支付宝结算信息");
			}
		} elseif ($type == 2) {
			if ($config["method"]["alipay"]["open"] == 0) {
				$this->response(\app\api\controller\Code::fail, "未开启支付宝结算渠道");
			}
			$minimum = $config["method"]["alipay"]["minimum"];
			$maximum = $config["method"]["alipay"]["maximum"];
			if (empty($user_settlement_info["alipay"]["account"]) || empty($user_settlement_info["alipay"]["name"]) || empty($user_settlement_info["alipay"]["qrcode"])) {
				$this->response(\app\api\controller\Code::fail, "您未设置支付宝结算信息");
			}
		} elseif ($type == 3) {
			if ($config["method"]["user_money"]["open"] == 0) {
				$this->response(\app\api\controller\Code::fail, "未开启结算到余额渠道");
			}
			$minimum = $config["method"]["user_money"]["minimum"];
			$maximum = $config["method"]["user_money"]["maximum"];
		} else {
			$this->response(\app\api\controller\Code::fail, "不支持的结算方式");
		}
		if ($arrival < $minimum || $maximum !== 0 && $maximum < $arrival) {
			$this->response(\app\api\controller\Code::fail, "您的{$user["point"]}个积分转换现金为{$amount}元(扣除手续费后:{$arrival}元)，未满足当前结算渠道的最大最小结算金额条件");
		}
		list($start_t, $end_t) = \think\helper\Time::today();
		if (\think\Db::name("app_member_user_settlement")->where([["uid", "=", $user["id"]], ["apply_time", ">=", $start_t], ["apply_time", "<", $end_t]])->count() >= $config["limit_count"]) {
			$this->response(\app\api\controller\Code::fail, "今日申请结算次数上限");
		}
		\think\Db::startTrans();
		try {
			$settlement_id = \think\Db::name("app_member_user_settlement")->insertGetId(["appid" => $this->project["id"], "uid" => $user["id"], "type" => $type, "amount" => $amount, "arrival" => $arrival, "service_fee" => $service_fee, "settlement_info" => $user["settlement_info"], "apply_time" => time()]);
			\app\api\common\member\Finance::set($user, "point", $user["point"], 1, "申请积分现金结算抵扣", \app\common\model\MemberAppModel::UserPointLogSourcePointCashSettlementDuration, $settlement_id);
			\think\Db::commit();
			$this->response(\app\api\controller\Code::success, "申请结算成功，请等待审核处理");
		} catch (\think\Exception $e) {
			\think\Db::rollback();
			$this->response(\app\api\controller\Code::error, $e->getMessage());
		}
	}
}